const fs = require('fs')
const path = require('path')

const bannerModel = require('../models/banner.model')

class BannerController {
	async list(ctx, next) {
    const result = await bannerModel.getBannerList()
    ctx.body = {
      returnCode: 200,
      data: result
    }
  }

  async BannerShowImgInfo(ctx, next) {
    const { bannerShowImg } = ctx.request.params

    let contentType = ''
    
    if (bannerShowImg.endsWith('.png')) {
      contentType = 'image/png'
    } else if (bannerShowImg.endsWith('.jpg') || bannerShowImg.endsWith('.jpeg')) {
      contentType = 'image/jpeg'
    }

    ctx.set('content-type', contentType)
    // 当前的 process.pwd() 为 `backend` 这个文件夹
    ctx.body = fs.createReadStream(`./public/images/banners/${bannerShowImg}`)

  }
}

module.exports = new BannerController()
